package com.asteros.crypto.utils.keystore;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.TrustAnchor;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ru.signalcom.io.TokenFile;
import ru.signalcom.io.TokenFileInputStream;
import ru.signalcom.io.TokenFileOutputStream;

public final class KeyStoreUtil
{
	public static KeyStore loadStore(String storeType) throws Exception
	{
		KeyStore store = KeyStore.getInstance(storeType);

		store.load(null, null);
		return store;
	}

	public static KeyStore loadStore(String storeType, String provider) throws Exception
	{
		KeyStore store = KeyStore.getInstance(storeType, provider);

		store.load(null, null);
		return store;
	}

	public static KeyStore loadStore(String storeType, String provider, String path, String pass) throws Exception
	{
		FileInputStream in = new FileInputStream(path);
		
		return loadStream(storeType, provider, in, pass);
	}

	public static KeyStore loadStore(String storeType, String provider, String path, String pass, String storageType) throws Exception
	{
		if (storageType == "RUTOKEN") {
			TokenFile file = new TokenFile(path);
			TokenFileInputStream in = new TokenFileInputStream(file);

			return loadStream(storeType, provider, in, pass);
		}else{
			return loadStore(storeType, provider, path, pass);
		}
	}

	private static KeyStore loadStream(String storeType, String provider, InputStream in, String pass) throws Exception{
		KeyStore store = KeyStore.getInstance(storeType, provider);

		store.load(in, pass.toCharArray());
		in.close();

		return store;
	}

	public static List<String> getContainerNames(KeyStore store) throws Exception
	{
		List cNames = new ArrayList();
		Enumeration en = store.aliases();
		while (en.hasMoreElements()) {
			String cName = (String)en.nextElement();
			cNames.add(cName);
		}
		return cNames;
	}

	public static KeyPair getKeyPair(KeyStore store, String contName, String password) throws Exception
	{
		PrivateKey keyA = (PrivateKey)store.getKey(contName, password.toCharArray());

		X509Certificate cert = (X509Certificate)store.getCertificate(contName);

		return new KeyPair(cert.getPublicKey(), keyA);
	}

	public static PublicKey getPublicKey(KeyStore store, String contName) throws Exception
	{
		X509Certificate cert = (X509Certificate)store.getCertificate(contName);

		return cert.getPublicKey();
	}

	public static Certificate getCertificate(KeyStore store, String contName) throws Exception
	{
		X509Certificate cert = (X509Certificate)store.getCertificate(contName);
		return cert;
	}
}
